import java.io.File;
import java.util.Scanner;

public class Demo14 {
    public static void main(String[] args) {
        System.out.println("请输入想扫描的文件");
        System.out.println("->");
        Scanner scanner = new Scanner(System.in);
        String rootPath = scanner.next();
        File file = new File(rootPath);
        System.out.println("请输入想删除的文件名字");;
        System.out.println("->");
        String key = scanner.next();
        scan(file,key);
    }

    private static void scan(File file, String key) {
        if(!file.isDirectory()){
            System.out.println("输入的路径有误！");
            return;
        }
        File[] files = file.listFiles();
        for (File f : files){
            if(f.isFile()){
                if(f.getName().contains(key)){
                    doDelete(f,key);
                }
            }else {
                scan(f,key);
            }
        }
    }

    private static void doDelete(File f,String k) {
        System.out.println(f.getAbsolutePath()+"是否删除  Y/N");
        Scanner scanner = new Scanner(System.in);
        String key = scanner.next();
        if(key.equals("Y")||key.equals("y")){
            f.delete();
        }
    }
}
